/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>Project: jse2204 - T11
 * <p>Powered by webrx On 2022-09-30 17:33:16
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T11 {
    volatile boolean f = true;

    void work(){
        System.out.printf("%s 启动计算中 %n", Thread.currentThread().getName());
        while(f){
            String[] o = new String[]{"", "+", "-"};
            Random rand = new Random();
            StringBuffer str = new StringBuffer("1");
            Pattern p = Pattern.compile("(\\d+|-\\d+)");
            while (f && true) {
                for (int i = 2; i < 10; i++)
                    str.append(String.format("%s%d", o[rand.nextInt(o.length)], i));
                String s = str.toString();
                Matcher m = p.matcher(s);
                List<Integer> ln = new ArrayList<>();
                while (m.find()) {
                    ln.add(Integer.parseInt(m.group()));
                }
                int sum = ln.stream().reduce(0, Integer::sum);
                if (sum == 100) {
                    System.out.printf("%s : %s%n",Thread.currentThread().getName(),s);
                    f = false;
                    break;
                }
                str.delete(1,str.length());
            }
        }
        System.out.printf("%s 结束 %n", Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        T11 t = new T11();
        for(int i=0;i<30;i++){
            new Thread(t::work,"t"+i).start();
        }
    }
}
